using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WiimoteLib;

namespace WiimoteAnaylse
{
    public partial class MainForm : Form
    {
        Wiimote wm = null;
        bool inWiimoteChanged;

        public MainForm()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                // create a new instance of the Wiimote
                wm = new Wiimote();
                // setup the event to handle insertion/removal of extensions
                wm.WiimoteExtensionChanged += new WiimoteExtensionChangedEventHandler(wm_WiimoteExtensionChanged);
                // setup the event to handle wiimote status change events
                wm.WiimoteChanged += new WiimoteChangedEventHandler(wm_WiimoteChanged);
                // connect to the Wiimote
                wm.Connect();
                // set the report type to return the IR sensor and accelerometer data (buttons always come back)
                wm.SetReportType(Wiimote.InputReport.IRAccel, true);
            }
            catch (Exception ex)
            {
                wm = null;
                MessageBox.Show(ex.Message, "ERROR");
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (wm != null)
                wm.WiimoteChanged -= wm_WiimoteChanged;
        }

        void wm_WiimoteExtensionChanged(object sender, WiimoteExtensionChangedEventArgs args)
        {
            if (args.Inserted)
                wm.SetReportType(Wiimote.InputReport.IRExtensionAccel, true);    // return extension data
            else
                wm.SetReportType(Wiimote.InputReport.IRAccel, true);            // back to original mode
        }

        void wm_WiimoteChanged(object sender, WiimoteChangedEventArgs args)
        {
            inWiimoteChanged = true;

            if (irViewer1.Visible)
                irViewer1.SetIRState(args.WiimoteState.IRState);
            if (accelViewer1.Visible)
                accelViewer1.SetAccelState(args.WiimoteState.AccelState);
            // outputs
            cbLED1.Checked = args.WiimoteState.LEDState.LED1;
            cbLED2.Checked = args.WiimoteState.LEDState.LED2;
            cbLED3.Checked = args.WiimoteState.LEDState.LED3;
            cbLED4.Checked = args.WiimoteState.LEDState.LED4;
            cbRumble.Checked = args.WiimoteState.Rumble;

            inWiimoteChanged = false;
        }

        private void btnIRViewer_Click(object sender, EventArgs e)
        {
            irViewer1.Visible = btnIRViewer.Checked;
        }

        private void btnAccelViewer_Click(object sender, EventArgs e)
        {
            accelViewer1.Visible = btnAccelViewer.Checked;
        }

        private void btnOutputs_Click(object sender, EventArgs e)
        {
            pnlOutputs.Visible = btnOutputs.Checked;
        }

        private void cbOutput_CheckedChanged(object sender, EventArgs e)
        {
            if (wm != null && !inWiimoteChanged)
            {
                if (sender == cbRumble)
                    wm.SetRumble(cbRumble.Checked);
                else
                    wm.SetLEDs(cbLED1.Checked, cbLED2.Checked, cbLED3.Checked, cbLED4.Checked);
            }
        }
    }
}